package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.NonMaidenHorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;

public class SpeedWinPlaceShowResolver extends AbstractResolver {
    private static final int MAX_RACES_FOR_WPS_CONSIDERATION = 5;
    private static final int MAX_RACES_FOR_SPEED_CONSIDERATION = 6;

    public SpeedWinPlaceShowResolver() {
    }

    void resolveSpeedAndWPS(NonMaidenHorseAnalysisResultImpl result, Race r, Horse h) {
        SpeedAndWPS speedAndWPS = new SpeedAndWPS();
        int raceCount = 0;
        for (PastPerformance perf : h.getPastPerformances()) {
            raceCount++;
            if (raceCount <= MAX_RACES_FOR_SPEED_CONSIDERATION) {

                Integer speed = getInteger(perf.getBrisSpeedRating(), 0);
                if (speed > speedAndWPS.maxSpeed) {
                    speedAndWPS.maxSpeed = speed;
                }
                if (speed >= getInteger(r.getBrisSpeedPar(), 0)) {
                    speedAndWPS.parCount++;
                }
            }
            if (raceCount <= MAX_RACES_FOR_WPS_CONSIDERATION) {
                Integer pos = getInteger(perf.getFinishPosition(), 99);
                if (pos == 1) {
                    speedAndWPS.wins++;
                } else if (pos == 2) {
                    speedAndWPS.places++;
                } else if (pos == 3) {
                    speedAndWPS.shows++;
                }
            }

        }
        result.setParSpeedCount(speedAndWPS.parCount);
        result.setSpeedFigure("" + speedAndWPS.maxSpeed);
        result.setRecentWins(speedAndWPS.wins);
        result.setRecentPlaces(speedAndWPS.places);
        result.setRecentShows(speedAndWPS.shows);
    }

    private final class SpeedAndWPS {
        protected int maxSpeed = 0;
        protected int parCount = 0;
        protected int wins = 0;
        protected int places = 0;
        protected int shows = 0;

    }
}